package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatementServiceImpl implements IStatementService {

    @Autowired
    private StatementMapper statementMapper;
    @Autowired
    private AppointmentMapper appointmentMapper;

    @Override
    public TablePageInfo<Statement> query(StatementQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Statement>(statementMapper.selectForList(qo));
    }


    @Override
    public void save(Statement statement) {
        // 新添加：消费中状态
        statement.setStatus(Statement.STATUS_CONSUME);
        statement.setCreateTime(new Date());
        statementMapper.insert(statement);
    }

    @Override
    public Statement get(Long id) {
        return statementMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Statement statement) {
        Statement statement1 = statementMapper.selectByPrimaryKey(statement.getId());
        if (!Statement.STATUS_CONSUME.equals(statement1.getStatus())) {
            // 只有不是消费中，不可以编辑
            throw new BusinessException("只有消费中状态才可以编辑。");
        }
        statementMapper.updateByPrimaryKey(statement);
    }

    @Override
    public void deleteBatch(Long id) {

        Statement statement = statementMapper.selectByPrimaryKey(id);
        if (!Statement.STATUS_CONSUME.equals(statement.getStatus())) {
            // 只有不是消费中，不可以编辑
            throw new BusinessException("只有消费中状态才可以删除。");
        }
        // 把预约单的状态改已到店
        appointmentMapper.cancel(statement.getAppointmentId(), Appointment.STATUS_ARRIVAL);
        statementMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Statement> list() {
        return statementMapper.selectAll();
    }

    @Override
    // 预约单id
    public Statement generateStatement(Long id) {
        // 获取预约单信息
        Appointment appointment = appointmentMapper.selectByPrimaryKey(id);
        // 查询结算单（不管什么状态）
        Statement allStatuStatement = statementMapper.selectAllByAppointmentId(id);
        //查询结算单
        Statement statement = statementMapper.selectByAppointmentId(id);
        // 查询出全部状态的结算单：
        if (allStatuStatement != null && Statement.STATUS_DELETE.equals(allStatuStatement.getStatus())) {
            // 如果本来已经生成了结算单，但是删除了结算单，现在又要再生成，把结算单的状态改回消费中即可
            statementMapper.changeStatusAgainById(id, Statement.STATUS_CONSUME);
            // 修改预约的状态，改为已生成结算单：
            appointmentMapper.cancel(id, Appointment.STATUS_SETTLE);
            // 修改结算单状态后，刷新 statement
            statement = statementMapper.selectByAppointmentId(id);
            // 如果是到店状态：结算单为空
        } else if (Appointment.STATUS_ARRIVAL.equals(appointment.getStatus()) && allStatuStatement == null) {
            // 创建结算单：
            statement = new Statement();
            statement.setCustomerName(appointment.getCustomerName());
            statement.setCustomerPhone(appointment.getCustomerPhone());
            statement.setAppointmentId(id);
            statement.setActualArrivalTime(appointment.getActualArrivalTime());
            statement.setLicensePlate(appointment.getLicensePlate());
            statement.setCarSeries(appointment.getCarSeries());
            statement.setInfo(appointment.getInfo());
            statement.setServiceType((long) appointment.getServiceType());
            statement.setCreateTime(new Date());
            statement.setStatus(Statement.STATUS_CONSUME);
            statementMapper.insert(statement);
            // 修改预约的状态，改为已生成结算单：
            appointmentMapper.cancel(id, Appointment.STATUS_SETTLE);
        }
        // 如果是已生成结算单的状态：直接返回结算单
        // 如果到店：创建结算单，并跳到 eidtDetail.html
        // 如果结算单已生成，不需要创建
        return statement;
    }

    @Override
    public void payStatement(Long statementId) {
        // 结算的条件：
        Statement statement = statementMapper.selectByPrimaryKey(statementId);
        if (Statement.STATUS_PAID.equals(statement.getStatus())) {
            throw new BusinessException("不能重复支付");
        }
        // 修改支付狀態，收款时间，收款人id,
        statement.setStatus(Statement.STATUS_PAID);
        statement.setPayTime(new Date());
        statement.setPayeeId(ShiroUtils.getUserId());
        statementMapper.changestatusById(statement);
    }
}
